package com.zuipin.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuipin.entity.product.ProductExtInfo;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IProductExtInfoService;
import com.zuipin.service.IProductInfoService;
import com.zuipin.util.Pagination;
import com.zuipin.util.RedisUtils;
import com.zuipin.vo.BackExtProDetailVo;
import com.zuipin.vo.BackProductPlatformSearchVo;
import com.zuipin.vo.BackProductPlatformVo;
import com.zuipin.vo.IntegralProSearchVo;
import com.zuipin.vo.IntegralProVo;
import com.zuipin.vo.ProductPropertyValueVo;
import com.zuipin.vo.SpecialPriceProVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 商品拓展信息Controller
 *
 * @ClassName ProductExtInfoController
 * @author wangcongyi
 * @date 2017年5月2日 上午10:04:10
 */
@Controller
@RequestMapping(value = "/productExtInfo")
@Api(value = "商品拓展信息", description = "商品拓展信息-wangcongyi")
public class ProductExtInfoController {
	@Resource
	private IProductExtInfoService	productExtInfoService;
	@Resource
	private IProductInfoService		productInfoService;
	
	@Resource
	private RedisUtils				redisUtils;
	
	@ResponseBody
	@RequestMapping(value = "/updateStock", method = RequestMethod.POST)
	@ApiOperation(value = "三期-更新库存-wangcongyi", notes = "更新库存")
	public Result<String> updateStock() throws CustomException {
		Result<String> result = new Result<>();
		productExtInfoService.updateStock();
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateStockBySkus", method = RequestMethod.POST)
	@ApiOperation(value = "根据skus手动同步库存", notes = "更新库存")
	@Valids({ @Valid(name = "proSkus", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "proSkus", value = "商品编号", required = true, dataType = "string", paramType = "query"), })
	public Result<String> updateStockBySkus(@RequestParam String proSkus) throws CustomException {
		Result<String> result = new Result<>();
		productExtInfoService.updateStockBySkus(proSkus);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/findProExtList", method = RequestMethod.POST)
	@ApiOperation(value = "三期-根据Skus查询商品拓展信息-wangcongyi", notes = "根据Skus查询商品拓展信息")
	public Result<?> findProExtBySkus() {
		Result<List<Map<String, Object>>> result = new Result<>();
		List<String> skus = new ArrayList<>();
		skus.add("3010601107");
		skus.add("3030602101");
		skus.add("3080606101");
		String device = "PC";
		Long sysId = 2l;
		result.setData(productExtInfoService.findProExtList(skus, device, sysId));
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/findProExt", method = RequestMethod.POST)
	@ApiOperation(value = "518-根据更新时间和系统ID获取商品信息", notes = "根据修改时间和系统ID获取商品信息")
	@Valids({ @Valid(name = "updateTime", required = true, maxLength = 19), @Valid(name = "sysId", required = true, maxValue = 9) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "updateTime", value = "更新时间", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<?> findProExt(@RequestParam String updateTime, @RequestParam Long sysId) {
		Result<List<Map<String, Object>>> result = new Result<>();
		JSONObject parmas = new JSONObject();
		parmas.put("updateTime", updateTime);
		parmas.put("sysId", sysId);
		result.setData(productExtInfoService.findProExt(parmas));
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/createProExt", method = RequestMethod.POST)
	@ApiOperation(value = "新增分系统商品信息(后台)", notes = "新增分系统商品信息(后台)")
	@Valids({ @Valid(name = "productInfo.id", required = true), @Valid(name = "productInfo.sysId", required = true), @Valid(name = "productInfo.proNo"),
			@Valid(name = "productInfo.proSku", required = true), @Valid(name = "productInfo.proName", required = true, maxLength = 500),
			@Valid(name = "productInfo.proShortName", required = true, maxLength = 200), @Valid(name = "productInfo.weight", required = true),
			@Valid(name = "productInfo.netWeight", maxValue = 9), @Valid(name = "productInfo.businessType", required = true, regex = "JOIN_SALES|SELF_SALES"),
			@Valid(name = "productInfo.proType", required = true, regex = "PRO|TEABAG|SAMPLE|EXPERIENCE"), @Valid(name = "productInfo.suppilerId"),
			@Valid(name = "productInfo.validDay"), @Valid(name = "productInfo.unint"), @Valid(name = "productInfo.specifications"), @Valid(name = "productInfo.isCanSplit"),
			@Valid(name = "productInfo.isVirtual"), @Valid(name = "productInfo.isDis"), @Valid(name = "productInfo.brandId"),
			@Valid(name = "price.productPrices.sysId", required = true), @Valid(name = "price.productPrices.priceType", required = true),
			@Valid(name = "price.productPrices.price", required = true) })
	public Result<?> createProExt(@RequestBody @ApiParam ProductExtInfo productExtInfo, @RequestBody @ApiParam ProductPropertyValueVo prop
	
	) {
		Result<Boolean> result = new Result<>();
		// JSONObject params = new JSONObject();
		// params.put("proSku", productInfo.getProSku());
		// params.put("proNo", productInfo.getProNo());
		// boolean status = false;
		// List<ProductInfo> checkInfos = null;
		// checkInfos = productInfoService.checkPro(params);
		// if (checkInfos.size() > 0) {
		// // 提示商品Sku或编码不唯一
		// result.setErrorCode(ErrorCode.OBJECT_EXIST);
		// result.setMessage("商品Sku或编码不唯一!");
		// result.setData(null);
		// } else {
		// status = productInfoService.insertXpPro(productInfo, price);
		// if (status) {
		// result.setErrorCode(ErrorCode.SUCCESS);
		// result.setMessage("体验品添加成功！");
		// result.setData(status);
		// } else {
		// result.setErrorCode(ErrorCode.ELSE);
		// result.setMessage("体验品添加失败！");
		// result.setData(status);
		// }
		//
		// }
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getIntegralPro", method = RequestMethod.POST)
	@Valids({ @Valid(name = "searchVo.device", required = true, regex = "PC|WAP"), @Valid(name = "searchVo.sysId", minValue = 1), @Valid(name = "page.pageNumber", required = true),
			@Valid(name = "page.pageSize", required = true) })
	@ApiOperation(value = "积分商城产品列表(前台)", notes = "积分商城产品列表(前台)")
	@ApiImplicitParams({ @ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query") })
	public Result<List<IntegralProVo>> getIntegralPro(@RequestBody IntegralProSearchVo searchVo, @Inject @ApiIgnore Pagination page) {
		
		PageResult<List<IntegralProVo>> result = new PageResult<>();
		List<IntegralProVo> list = productExtInfoService.getIntegralPro(searchVo, page);
		
		if (list != null && !list.isEmpty()) {
			result.setData(list);
			result.setTotal_record(page.getTotalRecord());
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setTotal_record(0);
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("没有积分商品");
		}
		return result;
	}
	
	// -----------------------------------------------------------
	/**
	 * 
	 * @param proExtId
	 * @param sysId
	 * @param state
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/modifyState", method = RequestMethod.POST)
	@ApiOperation(value = "上下架商品(后台)-huihang", notes = "上下架商品(后台)-huihang")
	@Valids({ @Valid(name = "proExtId", required = true, minValue = 1, maxValue = 999999999), @Valid(name = "sysId", required = true, maxValue = 9),
			@Valid(name = "state", required = true, regex = "ON_SHELF|OFF_SHELF") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "proExtId", value = "拓展信息ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "updateId", value = "修改人ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "state", value = "状态,ON_SHELF|OFF_SHELF", required = true, dataType = "string", paramType = "query") })
	public Result<?> modifyState(@RequestParam Long proExtId, @RequestParam Long sysId, @RequestParam String state, @RequestParam Long updateId) {
		Result result = productExtInfoService.productShelf(proExtId, sysId, state, updateId);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/backSearchPlatformPro", method = RequestMethod.GET)
	@ApiOperation(value = "后台查看平台商品列表-huihang", notes = "后台查看平台商品列表（查询全部）")
	@Valids({ @Valid(name = "sysId", required = true), @Valid(name = "page.pageNumber", required = true), @Valid(name = "page.pageSize", required = true),
			@Valid(name = "search.proType", regex = "PRO|EXPERIENCE"), @Valid(name = "search.state", regex = "ON_SHELF|OFF_SHELF|TIMING_ON_SHELF|TIMING_OFF_SHELF"),
			@Valid(name = "search.status", regex = "1|2"), @Valid(name = "search.isExchangeIntegral", regex = "0|1"),
			@Valid(name = "search.businessType", regex = "SELF_SALES|JOIN_SALES") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysId", value = "系统Id", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "search.catId", value = "品类ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "search.proSku", value = "商品编号", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "search.proName", value = "商品名称", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "search.proType", value = "商品类型 [商品：PRO],[体验品：EXPERIENCE]", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "search.state", value = "是否上架[上架：ON_SHELF],[下架：OFF_SHELF],[定时上架：TIMING_ON_SHELF],[定时下架：TIMING_OFF_SHELF]", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "search.status", value = "商品状态[1正常 ],[2 禁用]", required = false, dataType = "short", paramType = "query"),
			@ApiImplicitParam(name = "search.isExchangeIntegral", value = "是否积分兑换 [0：否][1：是]", required = false, dataType = "short", paramType = "query"),
			@ApiImplicitParam(name = "search.businessType", value = "商品模式  [自营 :SELF_SALES] , [联营:JOIN_SALES]", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public Result<List<BackProductPlatformVo>> backSearchPlatformPro(@RequestParam Long sysId, @Inject @ApiIgnore BackProductPlatformSearchVo search,
			@Inject @ApiIgnore Pagination page) {
		
		PageResult<List<BackProductPlatformVo>> result = productExtInfoService.findByProPlatformSearch(search, page, sysId);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/createOrUpdateExtPro", method = RequestMethod.POST)
	@ApiOperation(value = "后台添加(修改)平台商品SKU-huihang", notes = "后台添加(修改)平台商品SKU")
	@Valids({ @Valid(name = "backExtProDetailVo.proId", required = false), @Valid(name = "backExtProDetailVo.proSku", required = true),
			@Valid(name = "backExtProDetailVo.sysId", required = true), @Valid(name = "backExtProDetailVo.title", required = true),
			@Valid(name = "backExtProDetailVo.subTitle", required = false), @Valid(name = "backExtProDetailVo.buyLowLimit", required = true, maxLength = 7),
			@Valid(name = "backExtProDetailVo.buyUpperLimit", required = false, maxLength = 7),
			@Valid(name = "backExtProDetailVo.productDetails.device", required = true, regex = "PC|WAP"),
			@Valid(name = "backExtProDetailVo.productDetails.delFlag", required = false, regex = "true|false"),
			@Valid(name = "backExtProDetailVo.productDetails.updateId", required = false), //
			@Valid(name = "backExtProDetailVo.productDetails.content", required = true), //
			@Valid(name = "backExtProDetailVo.productImgs.imgUrl", required = true), @Valid(name = "backExtProDetailVo.productImgs.pcViewTag", required = true, regex = "0|1"),
			@Valid(name = "backExtProDetailVo.productImgs.wapViewTag", required = true, regex = "0|1"), })
	public Result createExtPro(@RequestBody @ApiParam(value = "添加平台SKU") BackExtProDetailVo backExtProDetailVo) throws CustomException {
		if (backExtProDetailVo.getProExtId() == null) {
			// 新增
			Result result = productExtInfoService.createExtPro(backExtProDetailVo);
			return result;
		} else {
			// 修改
			Result result = productExtInfoService.updateExtProDetail(backExtProDetailVo);
			return result;
		}
		
	}
	
	/*@ResponseBody
	@RequestMapping(value = "/updateExtProDetail", method = RequestMethod.POST)
	@ApiOperation(value = "后台修改平台商品SKU-huihang", notes = "后台修改平台商品SKU")
	@Valids({ @Valid(name = "backExtProDetailVo.proExtId", required = true), @Valid(name = "backExtProDetailVo.proNo", required = true),
			@Valid(name = "backExtProDetailVo.sysId", required = true), @Valid(name = "backExtProDetailVo.title", required = true),
			@Valid(name = "backExtProDetailVo.subTitle", required = true), @Valid(name = "backExtProDetailVo.buyLowLimi t", required = true),
			@Valid(name = "backExtProDetailVo.productDetails.device", required = true, regex = "PC|WAP"),
			@Valid(name = "backExtProDetailVo.productDetails.updateId", required = true), @Valid(name = "backExtProDetailVo.productDetails.content", required = true),
			@Valid(name = "backExtProDetailVo.productImgs.imgUrl", required = true), @Valid(name = "backExtProDetailVo.productImgs.pcViewTag", required = true),
			@Valid(name = "backExtProDetailVo.productImgs.wapViewTag", required = true), })
	public Result updateExtProDetail(@RequestBody @ApiParam(value = "修改平台商品") BackExtProDetailVo backExtProDetailVo) throws CustomException {
		
	}*/
	
	@ResponseBody
	@RequestMapping(value = "/findExtProDetail", method = RequestMethod.POST)
	@ApiOperation(value = "后台查询平台商品SKU详情-huihang", notes = "后台查询平台商品SKU详情")
	@Valids({ @Valid(name = "sysId", required = true), @Valid(name = "proSku", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysId", value = "系统Id", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "proSku", value = "商品编号", required = true, dataType = "string", paramType = "query"), })
	public Result<BackExtProDetailVo> findExtProDetail(@RequestParam Long sysId, @RequestParam String proSku) {
		Result<BackExtProDetailVo> result = productExtInfoService.findExtProDetail(sysId, proSku);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/timingModifyState", method = RequestMethod.POST)
	@ApiOperation(value = "定时上下架-huihang-huihang", notes = "定时上下架")
	@Valids({ @Valid(name = "proExtId", required = true, minValue = 1, maxValue = 999999999), @Valid(name = "sysId", required = true, maxValue = 9),
			@Valid(name = "state", required = true, regex = "ON_SHELF|OFF_SHELF"), @Valid(name = "timing", required = true) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "proExtId", value = "拓展信息ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "state", value = "状态,ON_SHELF|OFF_SHELF", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "timing", value = "定时时间", required = true, dataType = "string", paramType = "query"), })
	public Result timingModifyState(@RequestParam Long proExtId, @RequestParam Long sysId, @RequestParam String state, @RequestParam String timing) {
		return productExtInfoService.timingModifyState(proExtId, sysId, state, timing);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getSpecialPrice", method = RequestMethod.GET)
	@ApiOperation(value = "查询当前特价活动商品-huihang", notes = "查询当前特价活动商品")
	@Valids({ @Valid(name = "device", required = true, regex = "PC|WAP"), @Valid(name = "sysId", minValue = 1), @Valid(name = "page.pageNumber", required = true),
			@Valid(name = "page.pageSize", required = true), @Valid(name = "sysId", required = true) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "device", value = "设备PC|WAP", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query") })
	public Result<List<SpecialPriceProVo>> getSpecialPrice(String device, Long sysId, @Inject @ApiIgnore Pagination page) {
		return productExtInfoService.getSpecialPricePro(device, sysId, page);
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateAssessmentNum", method = RequestMethod.POST)
	@ApiOperation(value = "更新商品好评数-huihang", notes = "更新商品好评数")
	@Valids({ @Valid(name = "sysId", minValue = 1, required = true), @Valid(name = "proExtId", minValue = 1, required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "proExtId", value = "商品扩展ID", required = true, dataType = "long", paramType = "query"), })
	public Result updateAssessmentNum(Long sysId, Long proExtId) {
		return productExtInfoService.updateAssessmentNum(sysId, proExtId);
	}
	
}
